﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Adaptive_Clustering
{
    class Intensity : Estimations
    {
        bool EnableBilinear;
        public Intensity(bool EnableBilinear) 
        {
            this.EnableBilinear = EnableBilinear; 
        }

        public Byte[,] intensity(Byte[,] ImageClusterIndexArray, Byte[,] ImageArray, int k, int w)
        {
            int LeftBorder, RightBorder, x, NewPixel;
            Byte[,] NewImageArray = new Byte[ImageArray.GetLength(0), ImageArray.GetLength(1)];
            for (x = 0; x < ImageArray.GetLength(0); x++)
            {
                // Setting borders for the window scan
                LeftBorder = (x - (w / 2));
                if (LeftBorder < 0) LeftBorder = 0;
                RightBorder = (x + (w / 2));
                if (RightBorder > ImageArray.GetLength(0)) RightBorder = ImageArray.GetLength(0);
                Parallel.For(0, ImageArray.GetLength(1), y =>
                {
                   if (EnableBilinear && (y % 2 == 1) && (FarFromBorders(x, ImageArray.GetLength(0), y, ImageArray.GetLength(1))))
                    {
                        BiLinearInterpulation bl = new BiLinearInterpulation();
                        NewImageArray[x, y] = bl.biLinearInterpulation(ImageArray, x, y);
                    }
                    else
                    {
                        // Setting borders for the window scan
                        int TopBorder, ButtomBorder;
                        TopBorder = (y - (w / 2));
                        if (TopBorder < 0) TopBorder = 0;
                        ButtomBorder = (y + (w / 2));
                        if (ButtomBorder > ImageArray.GetLength(1)) ButtomBorder = ImageArray.GetLength(1);
                        ScanWindow scw = new ScanWindow();
                        NewPixel = scw.scanWindow(ImageClusterIndexArray, ImageArray, LeftBorder, RightBorder, TopBorder, ButtomBorder, ImageClusterIndexArray[x, y]);
                        if (NewPixel != (-1)) // if result of the scan is above the threshold
                            NewImageArray[x, y] = (Byte)NewPixel;
                        else //else remain with old value
                            NewImageArray[x, y] = ImageArray[x, y];
                    }
                });

            }
            return NewImageArray;
        }

    }
}
